<template>
  <div class="login">
    <div class="login_box">
      <span class="login_title">电商后台管理系统</span>
      <!-- 输入框 -->
      <el-form :model="loginForm" :rules="rules" ref="loginFromRef">
        <el-form-item prop="username">
          <el-input placeholder="请输入账户" v-model="loginForm.username">
            <i slot="prefix" class="iconfont icon-user"></i>
          </el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input type="password" placeholder="请输入密码" v-model="loginForm.password">
            <i slot="prefix" class="iconfont icon-3702mima"></i>
          </el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btn">
          <el-button type="primary" size="small" @click="login">登录</el-button>
          <el-button size="small" @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '123456',
      },
      rules: {
        username: [
          { required: true, message: '请输入账户', trigger: 'blur' },
          {
            min: 5,
            max: 10,
            message: '请输入正确的帐户',
            trigger: ['blur', 'change'],
          },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 5,
            max: 10,
            message: '请输入正确的密码',
            trigger: ['blur', 'change'],
          },
        ],
      },
    }
  },
  methods: {
    resetForm() {
      this.$refs.loginFromRef.resetFields()
    },
    login() {
      this.$refs.loginFromRef.validate((valid) => {
        if (!valid) return
        this.$http.post('login', this.loginForm).then((res) => {
          if (res.data.meta.status !== 200)
            return this.$message({
              message: res.data.meta.msg,
              type: 'error',
              duration: 2000,
            })
          window.sessionStorage.setItem('token', res.data.data.token)
          this.$router.push('/home')
          this.$message({
            message: '登录成功',
            type: 'success',
            duration: 2000,
          })
        })
      })
    },
  },
}
</script>

<style lang="less" scoped>
.login {
  height: 100%;
  background: linear-gradient(to right, #4b79a1, #283e51);
  position: relative;
}

.login_title {
  display: block;
  color: white;
  font-size: 32px;
  text-align: center;
  margin-bottom: 90px;
}
.login_box {
  width: 275px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -65%);
  .btn {
    display: flex;
    justify-content: space-between;
    padding-top: 10px;
    .el-button:nth-child(1) {
      margin-right: 20px;
    }
    .el-button {
      width: 100px;
    }
  }
  .el-form-item .el-input {
    font-size: 13px;
  }
}

.icon-user,
.icon-3702mima {
  margin-left: 5px;
}
</style>